using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using Ares.CommonObjects;

namespace cb0tServer
{
    class LinkAllowList
    {
        private static List<LinkAllowListObject> items = new List<LinkAllowListObject>();

        public static bool AddItem(ChannelListItem c)
        {
            foreach (LinkAllowListObject l in items)
            {
                if (l.name == c.name)
                {
                    l.ip1 = c.externalIp.GetAddressBytes()[0];
                    l.port = c.port;
                    Save();
                    return false;
                }
            }

            LinkAllowListObject n = new LinkAllowListObject();
            n.name = c.name;
            n.ip1 = c.externalIp.GetAddressBytes()[0];
            n.port = c.port;
            items.Add(n);
            Save();
            return true;
        }

        public static bool RemoveItem(String name)
        {
            for (int i = 0; i < items.Count; i++)
            {
                if (items[i].name == name)
                {
                    items.RemoveAt(i);
                    Save();
                    return true;
                }
            }

            return false;
        }

        public static String[] GetList()
        {
            if (items.Count == 0) return null;

            List<String> str = new List<String>();

            foreach (LinkAllowListObject l in items)
                str.Add(l.name);

            return str.ToArray();
        }

        public static bool CanAcceptLink(ChannelListItem c)
        {
            foreach (LinkAllowListObject l in items)
                if (l.name == c.name)
                    if (l.ip1 == c.externalIp.GetAddressBytes()[0])
                        if (l.port == c.port)
                            return true;

            return false;
        }

        public static void Load()
        {
            items.Clear();

            if (!File.Exists(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "data/linkallow.xml")) return;

            try
            {
                FileStream f = new FileStream(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "data/linkallow.xml", FileMode.Open);
                XmlReader xml = XmlReader.Create(new StreamReader(f));

                xml.MoveToContent();
                xml.ReadSubtree().ReadToFollowing("linkallows");

                while (xml.ReadToFollowing("item"))
                {
                    xml.ReadSubtree().ReadToFollowing("data");
                    LinkAllowListObject n = new LinkAllowListObject();
                    List<byte> b = new List<byte>(Convert.FromBase64String(xml.ReadElementContentAsString()));
                    n.ip1 = b[0];
                    b.RemoveAt(0);
                    n.port = BitConverter.ToUInt16(b.ToArray(), 0);
                    b.RemoveRange(0, 2);
                    n.name = Encoding.UTF8.GetString(b.ToArray());
                    items.Add(n);
                }

                xml.Close();
                f.Flush();
                f.Close();
            }
            catch { }
        }

        public static void Save()
        {
            try
            {
                XmlWriterSettings appearance = new XmlWriterSettings();
                appearance.Indent = true;
                XmlWriter xml = XmlWriter.Create(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "data/linkallow.xml", appearance);

                xml.WriteStartDocument();
                xml.WriteStartElement("linkallows");

                foreach (LinkAllowListObject l in items)
                {
                    List<byte> b = new List<byte>();
                    b.Add(l.ip1);
                    b.AddRange(BitConverter.GetBytes(l.port));
                    b.AddRange(Encoding.UTF8.GetBytes(l.name));
                    xml.WriteStartElement("item");
                    xml.WriteElementString("data", Convert.ToBase64String(b.ToArray()));
                    xml.WriteEndElement();
                }

                xml.WriteEndElement();
                xml.WriteEndDocument();
                xml.Flush();
                xml.Close();
            }
            catch { }
        }

    }

    class LinkAllowListObject
    {
        public byte ip1;
        public ushort port;
        public String name;
    }
}
